package com.my.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.domain.*;
import com.my.mapper.MyPaperMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MyPaperService extends ServiceImpl<MyPaperMapper, MyPaper> {

    @Resource
    private MyPerformanceService myPerformanceService;
    @Resource
    private MyPressureDataService myPressureDataService;
    @Resource
    private MyThicknessDataService myThicknessDataService;
    @Resource
    private MyTranslucencyService myTranslucencyService;


    public List<MyPaper> paperDetailList(String name) {
        List<MyPaper> papers = list(new QueryWrapper<>(MyPaper.class).eq(name != null,
                "name", name));
        if (CollUtil.isEmpty(papers)) {
            return new ArrayList<>();
        }
        return papers;
    }

    public void paperEdit(MyPaper paper) {
        if (ObjectUtil.isNull(paper.getId())) {
            this.save(paper);
            return;
        }
        updateById(paper);
    }

    public void paperDeleted(String id) {
        removeById(id);
        myPerformanceService.remove(new LambdaQueryWrapper<>(MyPerformance.class)
                .eq(MyPerformance::getPaperId, id));
        myPressureDataService.remove(new LambdaQueryWrapper<>(MyPressureData.class)
                .eq(MyPressureData::getPaperId, id));
        myThicknessDataService.remove(new LambdaQueryWrapper<>(MyThicknessData.class)
                .eq(MyThicknessData::getPaperId, id));
        myTranslucencyService.remove(new LambdaQueryWrapper<>(MyTranslucency.class)
                .eq(MyTranslucency::getPaperId, id));
    }
}
